﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EventLogger.Service;
using System.ServiceModel;
using System.Threading;

namespace RaiseEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            var parameters = new Arguments(args);

            string domain = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
            bool asUser = false;
            string eventName = null;

            if (parameters["domain"] != null)
            {
                domain = parameters["domain"];
            }

            if (parameters["user"] != null)
            {
                asUser = parameters["user"] == "true";
            }

            if (parameters["event"] != null)
            {
                eventName = parameters["event"];
            }

            Console.WriteLine("Raising event {0} {1}in domain {2}", eventName, asUser ? "as user " : "", domain);

            var client = LoggerServiceClient.ConnectToDefault(domain);

            string user = null;
            if (asUser)
            {
                user = string.Format("{0}\\{1}", Environment.UserDomainName, Environment.UserName);
            }

            var async = client.BeginRaiseEvent(eventName, Environment.MachineName, user, null, null, null);

            while (!async.IsCompleted)
            {
                Console.Write(".");
                Thread.Sleep(500);
            }
            client.EndRaiseEvent(async);

        }
    }
}
